package com.example.cloud.controller.system;

import com.example.cloud.constants.CommonConstants;
import com.example.cloud.constants.StateConstants;
import com.example.cloud.controller.common.BaseController;
import com.example.cloud.model.dto.QueryModel;
import com.example.cloud.model.dto.Result;
import com.example.cloud.model.system.SystemConfig;
import com.example.cloud.service.system.SystemConfigService;
import com.example.cloud.utils.ResultUtil;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by wangtao on 2018/1/26.
 */
@RestController
@RequestMapping("/sysConfig")
public class SystemConfigController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(SystemConfigController.class);

    @Autowired
    private SystemConfigService systemConfigService;

    @PostMapping("/search")
    public Result search(@RequestBody QueryModel model) {
        return this.singleQuery(model, SystemConfig.class);
    }

    @GetMapping("/findOne")
    public Result findOne(){
        return ResultUtil.success(systemConfigService.findOne());
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SystemConfig record){
        return ResultUtil.success(systemConfigService.doSave(record));
    }

    @GetMapping("/getSchedule/{type}")
    public Result getSchedule(@PathVariable int type) throws ParseException {
        List<List<String>> resultList = new ArrayList<>();
        SystemConfig config = systemConfigService.findOne();
        if(config == null){
            return ResultUtil.fail("系统参数未配置");
        }
        // 上班时间
        Date startDate = DateUtils.parseDate(config.getStartTime(), CommonConstants.DATE_FORMATS);
        // 下班时间
        Date endDate = DateUtils.parseDate(config.getEndTime(), CommonConstants.DATE_FORMATS);
        List<String> schedule = new ArrayList<>();
        if(type == StateConstants.RESERVATION_TYPE_OLD){
            if(config.getNurseInterval() == null){
                return ResultUtil.fail("护士预约时间间隔未配置");
            }
            getNextInterval(config.getNurseInterval(), startDate, endDate, schedule);
        }else if(type == StateConstants.RESERVATION_TYPE_NEW){
            if(config.getDoctorInterval() == null){
                return ResultUtil.fail("医生预约时间间隔未配置");
            }
            getNextInterval(config.getDoctorInterval(), startDate, endDate, schedule);
        }
        return ResultUtil.success(schedule);
    }

    /**
     * 递归获取预约时间点
     * @param interval
     * @param startDate
     * @param endDate
     */
    public void getNextInterval(int interval, Date startDate, Date endDate, List<String> list) {
        // 如果当前时间小于下班时间才可预约
        if(startDate.compareTo(endDate) < 0){
            list.add(DateFormatUtils.format(startDate, "HH:mm"));
            getNextInterval(interval, DateUtils.addMinutes(startDate, interval), endDate, list);
        }
    }

}
